//
//  WelComeViewController.swift
//  Weibo
//
//  Created by 张远文 on 16/4/27.
//  Copyright © 2016年 张远文. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit

class WelComeViewController: UIViewController {

    override func loadView() {
        view = backImage
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        setupUI()
        
    }
    
    //MARK: - 添加子视图
    private func setupUI() {
        
        //添加子视图
        view.addSubview(iconImage)
        view.addSubview(welcomeLabel)
        
        //设置布局约束
        iconImage.snp_makeConstraints { (make) in
            
            make.centerX.equalTo(view.snp_centerX)
            make.bottom.equalTo(view.snp_bottom).offset(-180)
            make.width.equalTo(90)
            make.height.equalTo(90)
        }
        
        welcomeLabel.snp_makeConstraints { (make) in
            
            make.top.equalTo(iconImage.snp_bottom).offset(20)
            make.centerX.equalTo(view.snp_centerX)
        }
        
        //网络加载用户图像
        iconImage.sd_setImageWithURL(UserAccountViewModel().headURL, placeholderImage: UIImage(named: "avatar_default_big"))
        
        //处理用户图像为圆形
        iconImage.layer.cornerRadius = 45
        iconImage.layer.masksToBounds = true
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        startAnimation()
    }
    
    private func startAnimation() {
        
        iconImage.snp_updateConstraints { (make) in
            make.bottom.equalTo(view.snp_bottom).offset(180 - view.bounds.height)
        }
        
        UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.9, initialSpringVelocity: 9.8, options: [], animations: { 
            
            self.view.layoutIfNeeded()
            }) { (_) in
                
            UIView.animateWithDuration(0.6, animations: { 
                
                self.welcomeLabel.alpha = 1
                }, completion: { (_) in
                    
                    //发出通知
                    NSNotificationCenter.defaultCenter().postNotificationName(AppSwitchRootViewController, object: nil)
            })
        }
    }
    
    override func prefersStatusBarHidden() -> Bool {
        return true
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    //MARK: - 懒加载所有视图控件
    //背景视图
    private lazy var backImage: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    //头像
    private lazy var iconImage: UIImageView = UIImageView()
    
    //欢迎提示语
    private lazy var welcomeLabel: UILabel = {
       
        let tip = UILabel()
        
        tip.textColor = UIColor.lightGrayColor()
        tip.font = UIFont.systemFontOfSize(19)
        tip.textAlignment = NSTextAlignment.Center
        tip.text = (UserAccountViewModel().userName ?? "") + "欢迎归来"
        tip.alpha = 0
        tip.sizeToFit()
        
        return tip
    }()
    
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
